/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Enumeration;
import java.math.*;
import java.lang.Math;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.j3d.Behavior;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.vecmath.AxisAngle4f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
public class RefreshBehavior extends Behavior{
    private TransformGroup targetTG;
    private TransformGroup targetTG2;
    private int flag=0;
    private Server tcpServer;
    private communicationBridge communicate;
    private Client connectionToPlayer;
    private int playerID;
    private Vector3f puckVelocity;
    //Glove5DT sensor = new Glove5DT("COM2");
    // create SimpleBehavior
    RefreshBehavior(TransformGroup targetTG,  int flag, Server tcpServer, communicationBridge communicate, int playerID){
        this.targetTG = targetTG;
        this.flag=flag;
        this.tcpServer = tcpServer;
        this.communicate = communicate;
        this.connectionToPlayer = null;
        this.playerID = playerID;
    }

    RefreshBehavior(TransformGroup targetTG,  int flag, Server tcpServer, communicationBridge communicate, int playerID, Vector3f puckVel){
        this.targetTG = targetTG;
        this.flag=flag;
        this.tcpServer = tcpServer;
        this.communicate = communicate;
        this.connectionToPlayer = null;
        this.playerID = playerID;
        this.puckVelocity=puckVel;
    }


    RefreshBehavior(TransformGroup puck, TransformGroup myHandle,  int flag, Client connectionToPlayer, communicationBridge communicate, Vector3f puckVel){
        //used to send data
    	this.targetTG = puck;
    	this.targetTG2 = myHandle; 
        this.flag=flag;
        this.tcpServer = null;
        this.communicate = communicate;
        this.connectionToPlayer = connectionToPlayer;
        this.puckVelocity = puckVel;
    }


    // initialize the Behavior
    //     set initial wakeup condition
    //     called when behavior beacomes live
    public void initialize(){
        // set initial wakeup condition
        this.wakeupOn(new WakeupOnElapsedFrames(0));
    }

    // called by Java 3D when appropriate stimulus occures
    public void processStimulus(Enumeration criteria){
        // decode event
        //get the data then move the object
        //if flag 0 do update other players handle
        //if flag 1 do my handle
    	//if flag 2 do read cords of puck from other player
        if(this.flag == 0) {
            if(this.tcpServer != null) {
                communicate.updateCordForObject(this.tcpServer,this.targetTG,"handle");
            } else {
                System.out.println("null =(");
            }
        }

        //problem with sending... need to somehow get my handle and puck. The other player can turn off reading puck when he is in control
        if(this.flag == 1) {
            if(this.connectionToPlayer != null) {
                if(this.connectionToPlayer.checkSend()) {
                    try {
                        //System.out.println("I can send data now");
                        communicate.sendWorldInfo(this.connectionToPlayer, targetTG, targetTG2, this.puckVelocity);
                        //System.out.println(this.puckVelocity);
                        //this.connectionToPlayer.restart();
                    } catch (IOException ex) {
                        Logger.getLogger(RefreshBehavior.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        if(this.flag == 2) {
            if(this.tcpServer != null) {
            	//check if i SHOULD update my puck
            	Transform3D checker = new Transform3D();
                //create vector to hold updated cordinates for object
            	//System.out.println();
                Vector3d hold = new Vector3d();
            	this.targetTG.getTransform(checker);
            	checker.get(hold);
            	if(this.playerID == 2) {
            		if(hold.z < 0){
            			communicate.updateCordForObject(this.tcpServer,this.targetTG,"puck");
            			communicate.updateVelocityForObject(this.tcpServer,this.puckVelocity,"velocity");
            			//System.out.println("puck on other player's side");
            			//System.out.println("hello2"+this.puckVelocity);
            		}
            	}
            	if(this.playerID == 1) {
            		if(hold.z >= 0){
            			communicate.updateCordForObject(this.tcpServer,this.targetTG,"puck");
            			communicate.updateVelocityForObject(this.tcpServer,this.puckVelocity,"velocity");
            			//System.out.println("hello"+this.puckVelocity);
            		}
            		//else {
            		//	System.out.println("puck on my side");
            		//}
            	}
            } else {
                System.out.println("null =(");
            }
        }
        
        
        this.wakeupOn(new WakeupOnElapsedFrames(0));
    }

}
